package com.docker.core.third.websocket;

import com.docker.core.mvc.Remark;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Record;
import org.apache.log4j.Logger;

import javax.websocket.Session;
import java.io.IOException;

/**
 * @Author ldl
 * @Create 2020/08/14 014 17:22
 */
@Remark("websocket处理工具")
public class WebsocketKit {

    private static Logger logger = Logger.getLogger(WebsocketKit.class);

    /**
     * @Param: [connectKey 连接键, data 推送的信息对象]
     * @return: void
     * @Author: ldl
     * @Create: 2020/08/16 016 15:20
     */
    @Remark("通过连接键标识推送信息")
    public static void sendMsg(String connectKey, Object data) {
        WebsocketPool socketPool = WebsocketPool.getSocketPool(connectKey);
        if (socketPool == null) {
            logger.info("未连接消息无法推送，连接键" + connectKey + "，推送的信息为：\n" + data);
            return;
        }
        sendMsg(socketPool.getSession(), null, data);
    }

    /**
     * @Param: [connectKey 连接键, cmd 命令码, data 推送的信息对象]
     * @return: void
     * @Author: ldl
     * @Create: 2020/08/16 016 15:20
     */
    @Remark("通过连接键标识推送信息")
    public static void sendMsg(String connectKey, String cmd, Object data) {
        WebsocketPool socketPool = WebsocketPool.getSocketPool(connectKey);
        if (socketPool == null) {
            logger.info("未连接消息无法推送，连接键" + connectKey + "，推送的信息为：\n" + data);
            return;
        }
        sendMsg(socketPool.getSession(), cmd, data);
    }

    /**
     * @Param: [session 连接session, cmd 命令码, data 推送的信息对象]
     * @return: void
     * @Author: ldl
     * @Create: 2020/08/16 016 15:25
     */
    @Remark("通过连接session推送信息")
    public static void sendMsg(Session session, String cmd, Object data) {
        if (StrKit.isBlank(cmd)) {
            cmd = WebsocketCMD.ON_MESSAGE;
        }
        try {
            session.getBasicRemote().sendText(
                    new Record().set("cmd", cmd).set("data", data).toJson());
        } catch (IOException e) {
            logger.error("消息推送失败，推送的信息为：\n" + data, e);
        }
    }

    /**
     * @Param: [connectKey 连接键]
     * @return: void
     * @Author: ldl
     * @Create: 2020/08/16 016 15:30
     */
    @Remark("通过应用标识关闭连接")
    public static void closeSocket(String connectKey) {
        closeSocket(WebsocketPool.getSocketPool(connectKey));
    }

    /**
     * @Param: [session 连接session]
     * @return: void
     * @Author: ldl
     * @Create: 2020/08/16 016 15:40
     */
    @Remark("通过连接session关闭连接")
    public static void closeSocket(Session session) {
        closeSocket(WebsocketPool.getSocketPool(session));
    }

    /**
     * @Param: [socketPool 连接池对象]
     * @return: void
     * @Author: ldl
     * @Create: 2020/08/16 016 15:41
     */
    @Remark("通过连接池关闭连接")
    private static void closeSocket(WebsocketPool socketPool) {
        if (socketPool == null) {
            return;
        }
        try {
            //删除连接池
            WebsocketPool.getPoolMap().remove(socketPool.getConnect_key());
            //关闭连接
            socketPool.getSession().close();
            logger.info("连接键" + socketPool.getConnect_key() + "连接关闭！");
        } catch (IOException e) {
            logger.error("websocket关闭异常，连接键" + socketPool.getConnect_key(), e);
        }
    }

}
